using System;
using System.Collections.Generic;
using System.IO;
using Spine;
using UnityEngine;

[Serializable]
public class DayRecord
{
	[Serializable]
	public class DayRecordList
	{
		public List<int> values = new List<int>();
	}

	private static DayRecord _instance;

	public const string RECORD_FILE = "record.dat";

	private string[] _cacheKeys;

	private Dictionary<string, int> _records = new Dictionary<string, int>();

	private long _lastDateValue;

	public static DayRecord instance
	{
		get
		{
			_instance = ((_instance != null) ? _instance : new DayRecord());
			return _instance;
		}
	}

	private bool isTheSameDay
	{
		get
		{
			DateTime now = DateTime.Now;
			DateTime value = DateTime.FromFileTime(_lastDateValue);
			return now.Subtract(value).Days == 0;
		}
	}

	private DayRecord()
	{
		_cacheKeys = Enum.GetNames(typeof(DayRecordKey));
		Clear();
	}

	public void Clear()
	{
		for (int i = 0; i < _cacheKeys.Length; i++)
		{
			string key = _cacheKeys[i];
			_records[key] = 0;
		}
		_lastDateValue = DateTime.Now.ToFileTime();
	}

	public void TodayAdd(DayRecordKey key, int addValue = 1)
	{
		if (!isTheSameDay)
		{
			Clear();
		}
		string key2 = key.ToString();
		if (_records.ContainsKey(key2))
		{
			int num = _records[key2];
			_records[key2] = num + addValue;
			Save();
		}
	}

	public int TodayGet(DayRecordKey key)
	{
		string key2 = key.ToString();
		if (!_records.ContainsKey(key2))
		{
			return 0;
		}
		if (!isTheSameDay)
		{
			Clear();
			return 0;
		}
		return _records[key2];
	}

	private void Save(BinaryWriter writer)
	{
		writer.Write(DateTime.Today.ToFileTime());
		foreach (KeyValuePair<string, int> record in _records)
		{
			writer.Write(record.Value);
		}
	}

	private void Load(BinaryReader reader)
	{
		_lastDateValue = reader.ReadInt64();
		for (int i = 0; i < _cacheKeys.Length; i++)
		{
			string key = _cacheKeys[i];
			_records[key] = reader.ReadInt32();
		}
	}

	private void Save()
	{
		DayRecordList dayRecordList = new DayRecordList();
		foreach (KeyValuePair<string, int> record in _records)
		{
			dayRecordList.values.Add(record.Value);
		}
		string json = JsonUtility.ToJson(dayRecordList);
		PlayerPrefs.SetString("record.dat", json);
		PlayerPrefs.Save();
		/* string text = StreamTools.GetPersistentDataPath() + "record.dat";
		string path = text.Substring(0, text.LastIndexOf('/'));
		if (!Directory.Exists(path))
		{
			Directory.CreateDirectory(path);
		}
		using (FileStream output = new FileStream(text, FileMode.Create))
		{
			BinaryWriter writer = new BinaryWriter(output);
			Save(writer);
		} */
	}

	public void Load()
	{
		string json = PlayerPrefs.GetString("record.dat");
		DayRecordList list = JsonUtility.FromJson<DayRecordList>(json);
		if (list == null) return;
		for (int i = 0; i < _cacheKeys.Length && i < list.values.Count; i++)
		{
			string key = _cacheKeys[i];
			_records[key] = list.values[i];
		}
		/* string path = StreamTools.GetPersistentDataPath() + "record.dat";
		if (File.Exists(path))
		{
			using (FileStream input = new FileStream(path, FileMode.Open))
			{
				BinaryReader reader = new BinaryReader(input);
				Load(reader);
			}
		} */
	}
}
